# coding: utf-8

'''
技能商城RemoteAPI接口
http://wiki.it.yzs.io:8090/pages/viewpage.action?pageId=27911312
'''

import time
from utils.req import *
from utils.jktest import *
# from test_case.usk_shop_remote_api_data import *

# 技能类别查询
# 分页查询技能类别（/rest/v1/skill_category/list）
# 分页查询技能列表（/rest/v1/skill/list）

# 分页查询单个技能多版本列表（/rest/v1/skill/list_version）
# 查询单个技能详情（/rest/v1/skill/detail)

def query_category(user_id):
    path = 'http://%s/%s/rest/v1/skill_category/list' % (Server, Project)
    params = {
        'userId' : user_id,
    }
    return send_get(path, params)

def query_list(user_id, type_=1, search_type=1, skill_origin='', keyword=''):
    '''
    搜索范围是技能商城页面下的.
    type            技能类型（1：自定义 2：智能问答 3：智能播报）
    searchType      查询类型（1：自己 2：技能商城上所有已上架）
    skillOrigin     技能来源（1：官方技能，2：第三方技能）
    categoryType    技能所属类别类型
    categoryId      技能类别ID
    keyword         搜索关键字
    tags            标签名（多个以英文逗号隔开）
    pageNum         页码
    pageSize        每页数量
    '''
    path = 'http://%s/%s/rest/v1/skill/list' % (Server, Project)
    params = {
        'userId' : user_id,
        # 'type' : type_,
        # 'searchType' : search_type,
        # 'skillOrigin' : skill_origin,
        # 'keyword' : '',
        # 'tags' : '' # ,资源
    }
    return send_get(path, params)


def query_list_version(user_id, skill_id):
    path = 'http://%s/%s/rest/v1/skill/list_version' % (Server, Project)
    params = {
        'userId' : user_id,
        'skillId' : skill_id,
    }
    return send_get(path, params)

def query_detail(user_id, skill_id, skill_version):
    path = 'http://%s/%s/rest/v1/skill/detail' % (Server, Project)
    params = {
        'userId' : user_id,
        'skillId' : skill_id,
        'skillVersion' : skill_version,
    }
    return send_get(path, params)




#########################################################

def check_api(api, ret, expected_output):
    unmatchs = get_unmatch_list(expected_output, ret)
    result = "Pass"
    if unmatchs:
        result = 'Fail'
        print "Fail %s" % api
        print unmatchs
        print ret
    Result_Map[result].append(api)


def check_api_list(Server, SkillID, SkillVersion, UserName="dakangz", UserId="4093"):
    
    ret = query_category(UserId)
    expected_output = "skillCategorys=[name=计算器&&name=记事本&&name=日历]"
    check_api('query_category', ret, expected_output)

    ret = query_list(UserId)
    expected_output = "userId=%s;userName=%s;skillId=%s;name=自动化测试;" % (UserId, UserName, SkillID)
    check_api('query_list', ret, expected_output)


    ret = query_list_version(UserId, SkillID)
    expected_output = "userId=%s;userName=%s;skillId=%s;name=自动化测试;" % (UserId, UserName, SkillID)
    check_api('query_list_version', ret, expected_output)


    ret = query_detail(UserId, SkillID, SkillVersion)
    expected_output = "userId=%s;userName=%s;skillId=%s;description＝自动化测试;" % (UserId, UserName, SkillID)
    check_api('query_detail', ret, expected_output)




if __name__ == '__main__':
    # user id : 登录开发者网站 > 打开F12 > Network > XHR > 我的应用页面 > 查找 creatorId.
    
    UserId = '4093'
    UserName = 'dakangz'
    Project = 'unios-usk-shop'
    Result_Map = {"Pass" : [], "Fail" : []}

    ENV = "uat"
    # 注意： 生产环境测试， 必须先上架“自动化测试”， 然后测试，测试完成后及时下架。

    Server = '172.18.10.28:8080' # UAT
    SkillID = '2e09f5b99d26424b8e5bb5561bbc9342' # 自动化测试
    SkillVersion = '1.0.19'  

    if ENV == "online":
        Server = '172.20.7.199:8080' # Production
        SkillID = '7323fb200c3141d5b956903e48b5a1e6' # 自动化测试
        SkillVersion = '1.0.8'

    check_api_list(Server, SkillID, SkillVersion)

    print Result_Map
    